package com.tanhua.service;

import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.util.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class MovementMQService {
    @Reference
    private PublishApi publishApi;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void sendSavePublishMsg(String publishId){
        sendMessage(publishId,1);
    }
    public void sendLikePublishMsg(String publishId){
        sendMessage(publishId,2);
    }
    public void sendLovePublishMsg(String publishId){
        sendMessage(publishId,3);
    }
    public void sendCommentPublishMsg(String publishId){
        sendMessage(publishId,4);
    }
    public void sendUnlikePublishMsg(String publishId){
        sendMessage(publishId,5);
    }
    public void sendUnlovePublishMsg(String publishId){
        sendMessage(publishId,6);
    }

    public void sendMessage(String publishId,Integer type){
        Publish publish = publishApi.findById(publishId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", UserHolder.getUser().getId());
        map.put("pid",publish.getPid());
        map.put("type",type);
        rocketMQTemplate.convertAndSend("tanhua-publish",map);
    }
}
